<template>
	<view class="home">
		<view class="back" @click="back">
			<image src="../../static/merchantInfo/back.svg" mode=""></image>
		</view>
		<view class="title">
			<!-- 照片 -->
			<view class="avatar">
				<view class="imgbox">
					<image class="img" src="@/static/LOGO.png">
					</image>
				</view>
			</view>
			<!-- 昵称 -->
			<view class="name">
				熙喆科技
				<!-- {{nikeName}} -->
			</view>
		</view>
		<view class="Information">
			<view class="rmation">
				商家信息录入
			</view>
			<!-- 表单 -->
			<view class="infofrom">
				<!-- 每一项 -->
				<view class="item">
					<view class="left">
						商家名称 :
					</view>
					<view class="right">
						<input type="text" v-model="shopList.shopsName" />
					</view>
				</view>
				<view class="item">
					<view class="left">
						商家地址 :
					</view>
					<view class="right">
						<input type="text" v-model="shopList.address" />
					</view>
				</view>
				<view class="item">
					<view class="left">
						经营类别 :
					</view>
					<view class="right">
						<!-- <input type="text" v-model="shopList.category" /> -->
						<uni-data-select v-model="shopList.category" :localdata="range" @change="change"
							placeholder="请选择经营类别"></uni-data-select>
					</view>
				</view>
				<view class="item">
					<view class="left" style="margin-left: 28rpx;">
						微信号 :
					</view>
					<view class="right">
						<input type="text" v-model="addweixin" />
					</view>
				</view>
				<scroll-view scroll-y="true">
					<template v-if="weixinList.length">
						<view class="weixinbox" style="width: 100%; height: 140rpx;">
							<view class="wxitem">
								<view class="wxnumber" v-for="(item,index) in weixinList" :key="index">
									{{item}}
								</view>
							</view>
						</view>
					</template>
				</scroll-view>
			</view>
			<!-- 添加多个微信 -->
			<view class="add" @click="add">
				<image src="../../static/upload.png" mode=""></image>
			</view>
			<!-- 上传照片 -->
			<view class="uploadimg">
				<!-- 预览图片 -->
				<!-- <view class="preview" v-if="shopList.advertise.length">
					<view class="previewitem" v-for="(item,index) in shopList.advertise" :key="index">
						<image :src="getImgUrl(item)" mode=""></image>
					</view>
				</view> -->
				<scroll-view scroll-y="true">
					<template v-if="prevList.length">
						<view class="preview">
							<view class="previewitem" v-for="(item,index) in prevList" :key="index">
								<image :src="item" mode=""></image>
							</view>
							<view class="preupload" @click="upload">
								<image src="../../static/upload.png" mode=""></image>
							</view>
						</view>
					</template>
					<view style="display: flex; justify-content: center; align-items: center;" v-else>
						<view class="upload" @click="upload">
							<image src="../../static/upload.png" mode=""></image>
						</view>
					</view>
				</scroll-view>
				<view class="uptext">
					点击上传照片
				</view>
				<view class="save" @click="save">
					保存
				</view>
			</view>
			<view class="collection" v-if="collection">
				<image src="../../static/Collection.jpg" mode="widthFix"></image>
			</view>
		</view>
		<view class="prompt" v-if="collection">
			<view class="prompt-text">
				扫码付款后录入
			</view>
			<view class="prompt-btn" @click="confirminput">
				确认录入
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		reactive,
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import
	getImgUrl
	from "@/utils/img.js"
	import {
		addShopApi,
		IndustryListApi
	} from "@/api/info.js"
	// 经营类别
	const change = (e) => {
		console.log('');
	}
	onMounted(() => {
		IndustryListApi().then(res => {
			range.value = res.rows
		})
	})
	let range = ref([])
	// 添加数据
	const shopList = ref({
		address: "", // 详细地址
		advertise: [], //  门店宣传图片
		category: "", // 类别
		city: "", // 市
		county: "", // 县
		createTime: "", //创建时间
		id: 0, //ID
		province: "", //省
		remark: "", //备注
		shopsLogo: "", //门店log
		shopsName: "", //商家名称
		uid: "", //用户ID
		updateTime: "", // 修改时间
		weixin: [] //微信号
	})
	//登传nikeName
	const nikeName = ref('')
	onLoad((option) => {
		nikeName.value = option.nikeName
	})
	// 添加微信
	let addweixin = ref('')
	let weixinList = reactive([])
	const add = () => {
		if (addweixin.value == '') {
			uni.showToast({
				title: "微信号不能为空",
				icon: "none"
			})
			return
		}
		weixinList.unshift(addweixin.value)
		uni.showToast({
			title: "微信号添加成功",
			icon: "success"
		})
		addweixin.value = ''
		shopList.value.weixin = weixinList
	}
	// 上传照片
	// 相册
	const prevList = ref([])
	const upload = () => {
		console.log('上传');
		uni.chooseImage({
			count: 9,
			sourceType: ['album'], //从相册选择
			success: function(res) {
				const uploadFilePath = res.tempFilePaths
				let filelist = []
				for (let i = 0; i < uploadFilePath.length; i++) {
					let obj = {}
					obj.name = "img" + i;
					obj.uri = uploadFilePath[i]
					filelist.push(obj)
					prevList.value.push(obj.uri)
				}
				// console.log('obj',obj)
				// 图片上传
				// for (let i = 0; i < uploadFilePath.length; i++) {
				// 	prevList.value.push(uploadFilePath[i])
				// uploadFile(filelist)
				// }
				uploadFile(filelist)
			},
			fail(err) {
				console.error('文件选择失败:', err)
			}
		})
	}
	// 文件上传
	const uploadFile = (filelist) => {
		uni.uploadFile({
			url: getImgUrl('front/upload/upimgs'),
			files: filelist,
			name: 'multipart',
			header: {
				'Authorization': localStorage.getItem('token')
			},
			formData: {
				'model': 'product',
				'pid': 8,
				'num': filelist.length
			},
			success(res) {
				if (JSON.parse(res.data)[0].code == 200) {
					const Data = JSON.parse(res.data)
					for (let i = 0; i < Data.length; i++) {
						console.log('Data[i].fileName', Data[i].fileName);
						shopList.value.advertise.push(Data[i].fileName)
					}
					uni.showToast({
						title: "上传成功",
						icon: "none"
					})
				} else {
					uni.showToast({
						title: "上传失败",
						icon: "none"
					})
				}
			},
			fail(err) {
				console.log('上传失败', err);
			}
		})
	}
	//  二维码
	const collection = ref(false)
	// 保存
	const save = () => {
		if (shopList.value.shopsName == '') {
			uni.showToast({
				title: "商家名称不能为空",
				icon: "none"
			})
			return
		}
		if (shopList.value.address == '') {
			uni.showToast({
				title: "商家地址不能为空",
				icon: "none"
			})
			return
		}
		if (!(/^[\u4e00-\u9fa5]{2,4}(省|市|区)+[\u4e00-\u9fa5]+(路|街|巷)/).test(shopList.value.address)) {
			uni.showToast({
				title: "请填写省市区精确到路的商家地址",
				icon: "none"
			})
			return
		}
		if (shopList.value.category == '') {
			uni.showToast({
				title: "经营类别不能为空",
				icon: "none"
			})
			return
		}
		if (shopList.value.weixin.length == '') {
			uni.showToast({
				title: "点击加号添加微信号",
				icon: "none"
			})
			return
		}
		if (shopList.value.advertise.length == '') {
			uni.showToast({
				title: "请上传门店图片",
				icon: "none"
			})
			return
		}
		collection.value = true

	}
	// 确认录入
	const confirminput = () => {
		addShopApi(shopList.value).then(res => {
			uni.showToast({
				title: "录入成功",
				icon: "none"
			})
			uni.navigateTo({
				url: '/pages/merchantInfo/index'
			})
		}).catch(err => {
			console.log('err', err);
		})

	}
	const back = () => {
		uni.navigateTo({
			url: '/pages/select/index'
		})
	}
</script>

<style lang="scss" scoped>
	.home {
		width: 100vw;
		height: 113vh;
		padding: 40rpx;
		background-image: url('../../static/bgimg.png');
		background-size: cover;
		background-position: center center;

		// 返回图标
		.back {
			width: 50rpx;
			height: 50rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			margin: 0 auto;
			display: flex;
			justify-content: start;
			margin-top: 30rpx;
			align-items: center;
			margin-left: 40rpx;

			.avatar {
				width: 140rpx;
				height: 140rpx;
				border: 1rpx solid #fff;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;

				.imgbox {
					width: 100rpx;
					height: 100rpx;

					.img {
						// border-radius: 50%;
						width: 100%;
						height: 100%;
					}
				}
			}

			.name {
				font-weight: 600;
				margin-left: 40rpx;
			}
		}

		.Information {
			width: 100%;
			background-color: #fff;
			height: 1050rpx;
			margin-top: 30rpx;
			border-radius: 20rpx;
			position: relative;

			//二维码
			.collection {
				width: 670rpx;
				position: absolute;
				left: 0;
				bottom: 0;

				image {
					width: 100%;
				}
			}

			.add {
				position: absolute;
				left: 90%;
				top: 32%;
				width: 40rpx;
				height: 40rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.rmation {
				width: 200rpx;
				height: 50rpx;
				background-color: #EBB4FF;
				border-radius: 0rpx 0rpx 30rpx 0rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background: linear-gradient(to right, #9CAAFF, #D4D0FF, #EAB1FF);
			}

			//表单
			.infofrom {
				padding: 20rpx 20rpx;

				.item {
					display: flex;
					justify-content: center;
					margin-bottom: 40rpx;
					align-items: center;

					.left {
						font-size: 30rpx;
						margin-right: 20rpx;
					}

					.right {
						width: 400rpx;
						border-bottom: 2rpx #DDDDDD solid;
					}
				}

				.wxitem {
					display: flex;
					justify-content: center;
					flex-wrap: wrap;
					font-size: 30rpx;

					.wxnumber {
						width: 200rpx;
						height: 50rpx;
						background-color: #EAEDFF;
						margin: 10rpx 18rpx;
						padding-left: 5rpx;
						overflow: hidden;
						line-height: 50rpx;
						border-radius: 5rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}

			}

			.uploadimg {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				// 预览图片
				.preview {
					display: flex;
					flex-wrap: wrap;
					justify-content: center;
					height: 230rpx;

					//预览上传
					.preupload {
						width: 100rpx;
						height: 100rpx;
						background-color: #EEEEEE;
						display: flex;
						justify-content: center;
						align-items: center;
						margin-top: 10rpx;

						image {
							width: 60rpx;
							height: 60rpx;
						}
					}

					.previewitem {
						width: 100rpx;
						height: 100rpx;
						margin: 10rpx 25rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}
				}

				.upload {
					width: 160rpx;
					height: 160rpx;
					background-color: #EEEEEE;
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 60rpx;
						height: 60rpx;
					}
				}

				.uptext {
					margin: 18rpx;
				}

				.save {
					width: 300rpx;
					height: 105rpx;
					background-color: #B18DFF;
					display: flex;
					justify-content: center;
					align-items: center;
					color: #fff;
					font-size: 48rpx;
					border-radius: 10rpx;
				}
			}
		}

		.prompt {
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin: 10rpx 0;

			.prompt-text {
				font-size: 45rpx;
			}

			.prompt-btn {
				font-size: 40rpx;
				width: 380rpx;
				height: 90rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 15rpx;
				border: 2rpx solid #E1E1E1;
				background-color: #fff;
			}
		}
	}
</style>